package com.dao.impl;

import com.dao.UsersDao;
import com.pojo.Users;
import com.utils.JDBCUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class UserDaoImpl implements UsersDao {
    @Override
    public List<Users> findAllUsers(int start,int limit) {
        List<Users> list=null;
        try{
            list =JDBCUtil.queryList(Users.class,"select * from s_user limit ?,?",start,limit);
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public Users login(Users users) {
        return JDBCUtil.queryOne(Users.class,"select * from s_user where username=? and password = ?",users.getUsername(),users.getPassword());
    }

    @Override
    public int deleteUser(String id) {
        return JDBCUtil.update("delete from s_user where id =?",id);
    }

    @Override
    public int addUser(Users users) {
        return JDBCUtil.update("INSERT INTO s_user VALUES(?,?,?,?,?);",users.getId(),users.getUsername(),users.getPassword(),users.getType(),users.getImgUrl());
    }

    @Override
    public Users findUsersByID(String id) {
        return JDBCUtil.queryOne(Users.class,"select * from s_user where id = ?",id);
    }

    @Override
    public int updateUser(Users users) {
        return JDBCUtil.update("update s_user set username=?,password=?,type=?,imgUrl=? where id =? ",users.getUsername(),users.getPassword(),users.getType(),users.getImgUrl(),users.getId());
    }

    @Override
    public List<Users> findUsername(String username) {
        return JDBCUtil.queryList(Users.class,"select * from s_user where username = ?",username);
    }

    @Override
    public int selectCount() {
        return JDBCUtil.queryCount("select count(*) from s_user");
    }
}
